package com.big.merchan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.big.cmn.DictFeignClient;
import com.big.merchan.mapper.MerchandiseSpecificationPackageMapper;
import com.big.merchan.service.MerchandiseSpecificationPackageService;
import com.big.model.model.merchandise.MerchandiseSpecificationPackage;
import com.big.model.vo.merchandise.PackageVo;
import com.big.model.vo.merchandise.SpecificationPackageVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.*;


@Service
@Slf4j
public class MerchandiseSpecificationPackageServiceImpl extends ServiceImpl<MerchandiseSpecificationPackageMapper, MerchandiseSpecificationPackage> implements MerchandiseSpecificationPackageService {


    @Autowired
    DictFeignClient dictFeignClient;


    /**
     * 规格价格组
     * @param mId
     * @return
     */
    @Override
    public SpecificationPackageVo speciFicationPackages(String mId) {
        List<PackageVo>packagesHdd= new ArrayList<>();
        List<PackageVo>packagesRunMemory= new ArrayList<>();

        QueryWrapper<MerchandiseSpecificationPackage> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(mId)){
            queryWrapper.eq("m_id",mId);
        }
        //降序
        queryWrapper.orderByDesc("total_price");
        //内存
        Map<Integer, Integer> dictHddMap=new HashMap<>();
        //
        Map<Integer, Integer> dictRunMemoryMap=new HashMap<>();

        List<MerchandiseSpecificationPackage> merchandisePackages = baseMapper.selectList(queryWrapper);
        merchandisePackages.forEach(item -> {
            this.specificationDictInfo(item);
            dictHddMap.put(item.getHdd(), item.getHdd());
            dictRunMemoryMap.put(item.getRunMemory(), item.getRunMemory());
        });


        for (Map.Entry<Integer, Integer> entry : dictHddMap.entrySet()) {
            packagesHdd.add(new PackageVo(entry.getKey(), dictFeignClient.getSpeName(String.valueOf(entry.getKey()))));
        }
        for (Map.Entry<Integer, Integer> entry : dictRunMemoryMap.entrySet()) {
            System.out.println(entry.getKey());
            packagesRunMemory.add(new PackageVo(entry.getKey(), dictFeignClient.getSpeName(String.valueOf(entry.getKey()))));
        }
        SpecificationPackageVo specificationPackage=new SpecificationPackageVo();
        specificationPackage.setSpecificationPackages(merchandisePackages);
        specificationPackage.getParam().put("hdd",packagesHdd);
        specificationPackage.getParam().put("runMemory",packagesRunMemory);
        specificationPackage.getParam().put("maxTotal",merchandisePackages.get(0).getTotalPrice());
        return specificationPackage;
    }

    @Override
    public MerchandiseSpecificationPackage findSpecificationPackage(String mId) {
        QueryWrapper<MerchandiseSpecificationPackage> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(mId)){
            queryWrapper.eq("m_id",mId);
        }
        MerchandiseSpecificationPackage merchandiseSpecificationPackage = baseMapper.selectOne(queryWrapper);
        return merchandiseSpecificationPackage;
    }

    public MerchandiseSpecificationPackage specificationDictInfo(MerchandiseSpecificationPackage specificationPackage){

        //硬盘规格
        String hddName = dictFeignClient.getSpeName(String.valueOf(specificationPackage.getHdd()));

        //运行内存
        String runMemoryName = dictFeignClient.getSpeName(String.valueOf(specificationPackage.getRunMemory()));

        //cpu
        String cpuName = dictFeignClient.getSpeName(String.valueOf(specificationPackage.getCpu()));

        //显卡
        String gpuName = dictFeignClient.getSpeName(String.valueOf(specificationPackage.getGpu()));

        specificationPackage.getParam().put("hddName",hddName);
        specificationPackage.getParam().put("runMemoryName",runMemoryName);
        specificationPackage.getParam().put("cpuName",cpuName);
        specificationPackage.getParam().put("gpuName",gpuName);
        log.info("这是组值"+specificationPackage);
        return specificationPackage;
    }



}
